// pages/court/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    courtId: 0,
    courtName: '',
    meCourtList: [],
    areaCourtList: [],
    courtList: [],
    provinceList: [],
    provinceIndex: 0,
    cityList: [],
    cityIndex: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var provinceTemp = []
    var cityTemp = []
    for (const i in app.globalData.areaList) {
      var item = app.globalData.areaList[i]
      if (i == 0) {
        item.checked = true
      } else {
        item.checked = false
      }
      provinceTemp.push(item)
      //第一条数据作为市默认=数据
      for (const j in item.child) {
        var city = item.child[j]
        if (j == 0) {
          city.checked = true
        } else {
          city.checked = false
        }
        cityTemp.push(city)
      }
    }

    if (app.globalData.courtId == null) {
      that.setData({
        courtName: '全部法院',
        courtId: null,
        provinceList: provinceTemp,
        cityList: cityTemp
      })
    } else {
      that.setData({
        courtName: app.globalData.courtName,
        courtId: app.globalData.courtId,
        provinceList: provinceTemp,
        cityList: cityTemp
      })
    }
    provinceTemp = null
    cityTemp = null
    that.loadCourtData()
  },
  /**
   * 返回
   */
  onBack: function() {
    wx.navigateBack()
  },
  /**
   * 确定按钮点击事件
   */
  onComplete: function() {
    var that = this
    var pages = getCurrentPages();
    //var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      courtName: that.data.courtName,
      courtId: that.data.courtId,
      page: 1,
      subjectList: []
    })
    app.globalData.courtId = that.data.courtId
    app.globalData.courtName = that.data.courtName
    prevPage.loadData()
    that.onBack()
  },
  /**
   * 点击tab切换
   */
  swichNav: function(e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {

      that.setData({
        currentTab: e.target.dataset.current
      })
      that.loadCourtData()
    }
  },
  /**
   * 加载数据列表
   */
  loadCourtData: function() {
    var that = this
    var temp = []
    if (that.data.currentTab == 0) {
      temp.push({
        id: "",
        name: '全部法院',
        checked: that.data.courtId == "" ? true : false
      })
      if (app.globalData.courtId != null) {
        temp.push({
          id: app.globalData.courtId == null ? "" : app.globalData.courtId,
          name: app.globalData.courtId == null ? "" : app.globalData.courtName,
          checked: that.data.courtId == app.globalData.userData.courtId ? true : false
        })
      }
    } else {
      var cityId = that.data.cityList[that.data.cityIndex].id
      for (const i in app.globalData.courtList) {
        var item = app.globalData.courtList[i]
        if (item.cityId == cityId) {
          if (that.data.courtId == item.id) {
            item.checked = true
          } else {
            item.checked = false
          }
          temp.push(item)
        }
      }
    }
    that.setData({
      courtList: temp,
      meCourtList: temp
    })
  },
  /**
   * 点击列事件
   */
  onCourt: function(e) {
    var that = this
    const index = parseInt(e.currentTarget.id)
    var name = ""
    var id = ""
    var temp = []
    for (const i in that.data.courtList) {
      var item = that.data.courtList[i]
      if (i == index) {
        item.checked = true
        name = item.name
        id = item.id
      } else {
        item.checked = false
      }
      temp.push(item)
    }
    that.setData({
      courtList: temp,
      courtName: name,
      courtId: id
    })
    temp = []
  },
  /**
   * 省选中事件
   * provinceIndex
   */
  onProvince: function(e) {
    var that = this
    const index = parseInt(e.currentTarget.id)
    if (index != this.data.provinceIndex) {
      var temp = []
      var cityTemp = []
      for (const i in this.data.provinceList) {
        var item = this.data.provinceList[i]
        if (i == index) {
          item.checked = true
        } else {
          item.checked = false
        }
        temp.push(item)
        //加载市级数据
        for (const j in item.child) {
          var city = item.child[j]
          if (j == 0) {
            city.checked = true
          } else {
            city.checked = false
          }
          cityTemp.push(city)
        }
      }
      that.setData({
        provinceIndex: index,
        cityIndex: 0,
        provinceList: temp,
        cityList: cityTemp
      })
    }
  },
  /**
   * 市选中事件
   * cityIndex
   */
  onCity: function(e) {
    var that = this
    const index = parseInt(e.currentTarget.id)
    if (index != this.data.cityIndex) {
      var temp = []
      for (const i in this.data.cityList) {
        var item = this.data.cityList[i]
        if (i == index) {
          item.checked = true
        } else {
          item.checked = false
        }
        temp.push(item)
      }
      that.setData({
        cityIndex: index,
        cityList: temp
      })
      that.loadCourtData()
      temp = null
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})